package com.xindaoapp.happypet.fragments;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xindao.photonics.fragments.manager.BaseFragment;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.InteralBanner;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.RollViewPager;

/**
 * 兑换中心
 * 
 * @author Administrator
 * 
 */
@Deprecated
public class ChangeCenterFragment2 extends BaseFragment {

    private TextView vMoreNiu;
    private ListView vChangelist;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_change_center, container, false);
    }

    @Override
    protected void initViews(View view) {
        super.initViews(view);
        vMoreNiu = (TextView) view.findViewById(R.id.moreniuniu);
        vChangelist = (ListView) view.findViewById(R.id.changelist);

        LayoutInflater inflater =
                (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewpager = inflater.inflate(R.layout.layout_change_item, null);

        // viewpager = (RelativeLayout) View.inflate(mActivity, , null);

        vViewpage = (LinearLayout) viewpager.findViewById(R.id.top_change_viewpager);
        vDescription = (TextView) viewpager.findViewById(R.id.top_change_title);
        vDotall = (LinearLayout) viewpager.findViewById(R.id.dots_ll);
        vChangelist.addHeaderView(viewpager);
        vChangelist.setAdapter(new MyAdapter());

        // ViewUtils.inject(this, viewpager);
    }

    private ArrayList<View> dotLists;
    // 轮播
    private RollViewPager rollViewPager;
    private RelativeLayout viewpager;
    private LinearLayout vViewpage;
    private TextView vDescription;
    private LinearLayout vDotall;

    // public void prepareViewPage(final Context context, final List<HomeBanner> list) {
    // List<String> titleList = new ArrayList<String>();
    // List<String> imageUrlList = new ArrayList<String>();
    // for (HomeBanner homeBanner : list) {
    // titleList.add(homeBanner.getTitle());
    // imageUrlList.add(homeBanner.getCoverpath());
    // }
    // initDots(list.size());
    // rollViewPager =
    // new RollViewPager(context, dotLists, R.drawable.point_normal,
    // R.drawable.point_enable, new RollViewPager.MyOnTouchCllickCallBack() {
    // @Override
    // public void OnTouchCllick(int position) {
    // String type = list.get(position % list.size()).getType();
    // if (type.equals("web")) {// 跳转到详情
    // Intent intent1 =
    // new Intent(context, HomeBannerDetailActivity.class);
    // intent1.putExtra("url", list.get(position % list.size())
    // .getLink());
    // context.startActivity(intent1);
    // getActivity().overridePendingTransition(
    // R.anim.new_dync_in_from_right,
    // R.anim.new_dync_out_to_left);
    // } else if (type.equals("appthread")) {// 跳转到帖子详情
    // Intent intent2 = new Intent(context, PostDetailActivity.class);
    // intent2.putExtra("postID", list.get(position % list.size())
    // .getLink());
    // context.startActivity(intent2);
    // getActivity().overridePendingTransition(
    // R.anim.new_dync_in_from_right,
    // R.anim.new_dync_out_to_left);
    // }
    // }
    // });
    // rollViewPager.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
    // LayoutParams.WRAP_CONTENT));
    // rollViewPager.setTitle(tvDescription, titleList);
    // rollViewPager.setImageUrl(imageUrlList);
    // rollViewPager.startRoll();
    // mViewPager.removeAllViews();
    // mViewPager.addView(rollViewPager);
    // }

    private void initDots(int size) {
        dotLists = new ArrayList<View>();
        vDotall.removeAllViews();
        for (int i = 0; i < size; i++) {
            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(CommonUtil.dip2px(mActivity, 10),
                            CommonUtil.dip2px(mActivity, 10));
            layoutParams.setMargins(5, 0, 5, 0);
            // LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(15,
            // 15);

            // lp.leftMargin = 10;

            View m = new View(mActivity);
            if (i == 0) {
                m.setBackgroundResource(R.drawable.point_enable);
            } else {
                m.setBackgroundResource(R.drawable.point_normal);
            }
            m.setLayoutParams(layoutParams);
            dotLists.add(m);
            vDotall.addView(m);
        }
    }

    @Override
    protected void initEvents(View view) {
        super.initEvents(view);
    }

    @Override
    protected void initEvents() {
        super.initEvents();
    }

    private final boolean isFirst = true;

    @Override
    protected void onLoadData() {
        super.onLoadData();
        getMoccaApi().getChangePager(new IRequest<List<InteralBanner>>() {

            @Override
            public void request(List<InteralBanner> t) {
                processData(t);
            }

        });
    }

    private List<String> imageViewUrlLists;
    private RollViewPager mRollViewPager;

    protected void processData(List<InteralBanner> t) {
        List<String> titleList = new ArrayList<String>();
        if (t != null) {

            // 初始化点
            initDots(t.size());
            // 图片的url地址集合
            imageViewUrlLists = new ArrayList<String>();
            // 文本描述的集合
            // titleLists = new ArrayList<String>();
            // 把图片的地址和文本描述初始化
            for (InteralBanner interal : t) {
                imageViewUrlLists.add(interal.coverpath);
                // titleLists.add(topnews.title);
            }
            mRollViewPager =
                    new RollViewPager(mActivity, dotLists, R.drawable.point_normal,
                            R.drawable.point_enable, new RollViewPager.MyOnTouchCllickCallBack() {

                                @Override
                                public void OnTouchCllick(int position) {

                                }
                            });

            LinearLayout.LayoutParams layoutParams =
                    new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT);

            mRollViewPager.setImageUrl(imageViewUrlLists);

            mRollViewPager.setTitle(vDescription, titleList);

            mRollViewPager.startRoll();

            viewpager.addView(mRollViewPager);

        }
    }

    @Override
    protected String getTitleName() {
        return "兑换中心";
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public Object getItem(int arg0) {
            return null;
        }

        @Override
        public long getItemId(int arg0) {
            return 0;
        }

        @Override
        public boolean isEnabled(int position) {
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup arg2) {
            convertView = View.inflate(mActivity, R.layout.item_changecenter, null);
            ImageView img1 = (ImageView) convertView.findViewById(R.id.img1);
            ImageView img2 = (ImageView) convertView.findViewById(R.id.img2);
            img1.setImageResource(R.drawable.all158);
            img2.setImageResource(R.drawable.all292);
            return convertView;
        }

    }

}
